<?php

class Statistic_IndexController extends Zend_Controller_Action
{

    protected $_itemMapper;
    protected $_orderMapper;

    public function init()
    {
        /* Initialize action controller here */
        $this->_itemMapper = new Application_Model_ItemMapper();
        $this->_orderMapper = new Application_Model_CustomerOrderMapper();
    }

    public function indexAction()
    {
        set_time_limit(0);
        $turnovers = array(
            2006    => 0,
            2007    => 0,
            2008    => 0,
            2009    => 0
        );
        foreach($turnovers as $key => $value){
            $begin = mktime(0,0,0,03,01,$key);
            $endYear = $key + 1;
            $end =  mktime(23,59,59,02,28,$endYear);
            $turnovers[$key] = $this->_orderMapper->calculateTurnover($begin,$end);
        }
        $this->view->turnovers = $turnovers;

        $handle_csv = fopen('BECANERIE.txt', "r");
        $items = array();        
        while ($data = fgetcsv($handle_csv, 4096, ";")){
            $item = new Application_Model_Item(array(
                'reference' => $data[0]
            ));
            // recherche des commandes contenant l'article
            $this->_itemMapper->findItemOrdersByReference($item);

            // recherche des avoirs contenant l'article
            $this->_itemMapper->findItemCreditNotesByReference($item);
            
            $items[] = $item;

        }
        
        $this->view->items = $items;
    }


}

